/**
 * @author KujoMiva
 * @name PageBlankScreen.ts
 * @desc 空白页面界面
 * @time 2020年03月01日 21:01:12 星期天
 */
class PageBlankScreen extends egret.DisplayObjectContainer {
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.createBg()
    this.createBody()
    this.createDate()
    this.settingSelf()
  }

  private settingSelf() {
    Utils.anchorCenter(this)
    this.x = this.stage.stageWidth / 2
    this.y = this.stage.stageHeight / 2
  }

  private createBg() {
    const bg = Utils.createBitmapByName('book_bg_png')
    this.addChild(bg)

    this.stage.addEventListener('over', () => {
      bg.texture = RES.getRes(`page_end_all_png`)
    }, this)
    this.stage.addEventListener('before', () => {
      bg.texture = RES.getRes(`page_0_all_png`)
    }, this)
    this.addEventListener('next', () => {
      bg.texture = RES.getRes(`book_bg_png`)
    }, this)
  }

  private createBody() {
    const nowDay = DayData.data[ParamsPool.NOW_PAGE_INDEX].day
    console.log(nowDay)
    let content = new eui.Image(RES.getRes(`content_${nowDay.replace('.', '_')}_png`))
    if (nowDay === '尾声') {
      this.stage.dispatchEvent(new egret.Event('over'))
    } else if (nowDay === '前言') {
      this.stage.dispatchEvent(new egret.Event('before'))
    } else {
      this.dispatchEvent(new egret.Event('next'))
    }
    const group = new eui.Group()
    group.addChild(content)
    const scrollView = new eui.Scroller()
    this.addChild(scrollView)
    scrollView.viewport = group
    scrollView.width = 550
    scrollView.height = 660
    Utils.anchorCenter(scrollView)
    scrollView.x = this.stage.stageWidth / 2
    scrollView.y = (this.stage.stageHeight / 2) - 90
    scrollView.alpha = 0

    egret.Tween.get(scrollView).to({ alpha: 1 }, 200)
    this.addEventListener('hideContent', () => {
      egret.Tween.get(scrollView)
        .to({ alpha: 0 }, 200)
        .call(() => {
          this.dispatchEvent(new egret.Event('pageHideOver'))
        })
    }, this)
  }

  private createDate() {
    const nowDay = DayData.data[ParamsPool.NOW_PAGE_INDEX].day
    if (nowDay === '尾声') {
      return
    }
    const date = Utils.createBitmapByName(`date_${nowDay.replace('.', '_')}_png`)
    this.addChild(date)
    date.x = 60
    date.y = 100
    this.addEventListener('hideContent', () => {
      egret.Tween.get(date).to({ alpha: 0 }, 200)
    }, this)
  }

  public hideContent() {
    this.dispatchEvent(new egret.Event('hideContent'))
  }

}